package com.ddstation.common.initialization;

import com.ddstation.common.property.*;
import com.ddstation.security.property.WnSecretKey;
import com.ddstation.verifycode.property.VerifyCodeProperties;
import com.qq.weixin.property.WxProperty4EnterpriseStaff;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServlet;

@SuppressWarnings("serial")
public class PropertiesLoader extends HttpServlet implements ServletContextListener {
	
	private static final Logger logger =
            LoggerFactory.getLogger(PropertiesLoader.class);
	//用于在容器关闭时,操作
	public void contextDestroyed(ServletContextEvent sce) {
	   
	}
	
	//用于在容器开启时,操作
	public void contextInitialized(ServletContextEvent sce) {
		
		String common = sce.getServletContext().getInitParameter("properties-common");
		String wnlis = sce.getServletContext().getInitParameter("properties-invs");

		try {
			// 外部公共配置
			EnvironmentProperties.initialize(common);
			WnSecretKey.initialize(common); // 秘钥
			WxProperty4EnterpriseStaff.initialize(common); // 必须放在秘钥之后
			
			SystemProperties.initialize(wnlis);
			AliCloudOssProperties.initialize(common);// 阿里云服务
			// 工程特有设置
			JsVersionProperties.initialize(wnlis);
			GlobalExceptionAlarmProperties.initialize(wnlis);
			VerifyCodeProperties.initialize(wnlis);

		} catch(Exception e) {
			logger.error(e.getMessage());
			logger.error(e.getStackTrace().toString());
//			throw new RuntimeException(e);
			System.exit(-1);
		}
	}
}
